package dao;

import utils.Book;
import utils.ConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;


public class BookDao implements IBook {
    private Connection con;
    private PreparedStatement ps;
    private  ResultSet rst;
    private ConnectionManager connectionManager = new ConnectionManager();
    public boolean insert(Book book) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("INSERT INTO t_book(name,author,publisher,price,url,discount) VALUES (?,?,?,?,?,?)");
            ps.setString(1,book.getName());
            ps.setString(2,book.getAuthor());
            ps.setString(3,book.getPublisher());
            ps.setDouble(4,book.getPrice());
            ps.setString(5,book.getUrl());
            ps.setDouble(6,book.getDiscount());
            int i = ps.executeUpdate();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    public boolean delete(int bookid) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("DELETE FROM t_book where bookid=?");
            ps.setInt(1,bookid);
            int i = ps.executeUpdate();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    public boolean update(Book book) {
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("UPDATE t_book set name=?,author=?,publisher=?,price=?,url=?,discount=? where bookid=?");
            ps.setString(1,book.getName());
            ps.setString(2,book.getAuthor());
            ps.setString(3,book.getPublisher());
            ps.setDouble(4,book.getPrice());
            ps.setString(5,book.getUrl());
            ps.setDouble(6,book.getDiscount());
            ps.setInt(7,book.getBook_id());
            int i = ps.executeUpdate();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }

    public Book findbyname(String book_name) {
        Book book=null;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("select * from t_book where name=?");
            ps.setString(1,book_name);
            rst = ps.executeQuery();
            if(rst.next()){
                book = setBook(rst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return book;
    }

    public Book findbyId(int book_id) {
        Book book=null;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("select * from t_book where bookid=?");
            ps.setInt(1,book_id);
            rst = ps.executeQuery();
            if(rst.next()){
                book = setBook(rst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return book;
    }

    public ArrayList<Book> find(String author) {
        ArrayList<Book> books = new ArrayList<Book>();
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("select * from t_book WHERE author=?");
            ps.setString(1,author);
            rst = ps.executeQuery();
            while (rst.next()){
                Book book = setBook(rst);
                books.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return books;
    }

    public ArrayList<Book> findAll() {
        ArrayList<Book> books = new ArrayList<Book>();
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("select * from t_book");
            rst = ps.executeQuery();
            while (rst.next()){
                Book book = setBook(rst);
                books.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return books;
    }

    public Book setBook(ResultSet rst) throws Exception {
        Book book = new Book();
        book.setBook_id(rst.getInt("bookid"));
        book.setName(rst.getString("name"));
        book.setAuthor(rst.getString("author"));
        book.setPublisher(rst.getString("publisher"));
        book.setPrice(rst.getDouble("price"));
        book.setUrl(rst.getString("url"));
        book.setDiscount(rst.getDouble("discount"));
        return book;
    }

    public boolean setMoney(){
        boolean flag=false;
        try {
            con=connectionManager.getCon();
            ps = con.prepareStatement("UPDATE t_book set price=price*discount");
            int i = ps.executeUpdate();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            connectionManager.closeAll(con,ps,null);
        }
        return flag;
    }
}
